package com.tubang.organization.controller;


import com.sun.tools.javac.code.Attribute;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.ResultData;
import com.tubang.component.util.RequestUtil;
import com.tubang.organization.entity.RoleMenuEntity;
import com.tubang.organization.service.IRoleMenuService;
import com.tubang.organization.vo.BindingRMVo;
import com.tubang.organization.vo.MenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 角色-菜单 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2017-11-29
 */
@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {
    private Logger logger = LoggerFactory.getLogger(getClass());


    @Resource
    private IRoleMenuService roleMenuService;

    /**
     * 角色 菜单 关系绑定
     *
     * @return
     */
    @PostMapping(value = "bindingRM")
    public ResultData bindingRM(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);

            BindingRMVo bindingRMVo = GsonQuick.toObject(receivePostJson, BindingRMVo.class);
            //删除与此角色id相关的所有关系
            roleMenuService.deleteByRoleId(bindingRMVo.getRoleId());
            List<MenuVo> menus = bindingRMVo.getMenus();
            Timestamp now = new Timestamp(System.currentTimeMillis());
            for (MenuVo menuVo : menus) {
                RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
                roleMenuEntity.setRoleId(bindingRMVo.getRoleId());
                roleMenuEntity.setMenuId(menuVo.getId());
                roleMenuEntity.setCreateTime(now);
                roleMenuEntity.setUpdateTime(now);
                roleMenuEntity.setMenuLev(Constants.DEFAULT_VAULE_ONE);
                roleMenuEntity.setEnable(true);
                roleMenuService.insertOrUpdate(roleMenuEntity);
                //保存二级菜单
                List<MenuVo> childList = menuVo.getChildList();

                for (MenuVo childVo : childList) {
                    RoleMenuEntity childRoleMenu = new RoleMenuEntity();
                    childRoleMenu.setRoleId(bindingRMVo.getRoleId());
                    childRoleMenu.setMenuId(childVo.getId());
                    childRoleMenu.setCreateTime(now);
                    childRoleMenu.setUpdateTime(now);
                    childRoleMenu.setParentId(roleMenuEntity.getId());
                    childRoleMenu.setMenuLev(Constants.DEFAULT_VAULE_TWO);
                    childRoleMenu.setEnable(true);
                    roleMenuService.insertOrUpdate(childRoleMenu);
                }

            }
            return ResultData.result(true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[RoleController]==> method[getRoleList],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }



    /**
     * 角色 菜单 查询角色的菜单  带权限标识
     *
     * @return
     */
    @GetMapping(value = "selectRMByRIdHave")
    public ResultData selectRMByRIdHave(@RequestParam(required = true) String roleId,@RequestParam(required = true) String userType) {
        try {
            List<MenuVo> menuVos = roleMenuService.selectRMByRIdHave(roleId,userType);
            return ResultData.result(true).setData(menuVos);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[RoleController]==> method[selectRMByRId],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }


    /**
     * 角色 菜单 查询角色的菜单
     *
     * @return
     */
    @GetMapping(value = "selectRMByRId")
    public ResultData selectRMByRId(@RequestParam(required = true) String roleId) {
        try {
            List<MenuVo> menuVos = roleMenuService.selectRMListByRoleId(roleId);
            return ResultData.result(true).setData(menuVos);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[RoleController]==> method[selectRMByRId],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }

    /**
     * 查询该角色下所有的菜单
     *
     * @return
     */
    @GetMapping(value = "selectMenuListByRId")
    public ResultData selectMenuListByRId(@RequestParam(required = true) String roleId) {
        try {
            List<MenuVo> menuVos = roleMenuService.selectMenuListByRoleId(roleId);
            return ResultData.result(true).setData(menuVos);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[RoleController]==> method[selectRMByRId],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }

}
